package org.breathe.dynamic.thread.pool.core.notify.wechat;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.breathe.dynamic.thread.pool.core.context.BreatheDtpContext;
import org.breathe.dynamic.thread.pool.core.context.BreatheDtpContextHolder;
import org.breathe.dynamic.thread.pool.core.event.AlarmEvent;
import org.breathe.dynamic.thread.pool.core.executor.ThreadPoolExecutorRegistry;
import org.breathe.dynamic.thread.pool.core.thread.BreatheDtpExecutor;
import org.breathe.thread.pool.common.constant.WechatNotifyConstant;
import org.breathe.thread.pool.common.entity.BreatheDtpMainProperties;
import org.breathe.thread.pool.common.entity.BreatheDtpProperties;
import org.breathe.thread.pool.common.entity.ThreadPoolExecutorProperties;
import org.breathe.thread.pool.common.enums.NotifyPlatformEnum;
import org.breathe.thread.pool.common.enums.NotifyTypeEnum;
import org.breathe.thread.pool.common.utils.ApplicationContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 动态线程池微信通知
 * @author: breathe
 * @createTime: 2025-04-13
 */
@Slf4j
@Component
public class BreatheDtpWechatNotifier extends AbstractWechatNotifier {
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;
    @Autowired
    private BreatheDtpProperties breatheDtpProperties;
    @Override
    public String platform() {
        return NotifyPlatformEnum.WECHAT.name().toLowerCase();
    }

    @Override
    public void sendChangeMsg(BreatheDtpMainProperties oldProperties, List<String> differences) {
        BreatheDtpContext breatheDtpContext = BreatheDtpContextHolder.get();
        breatheDtpContext.getPlatform(NotifyPlatformEnum.WECHAT.name()).forEach(platform -> {
            String content = buildNoticeContent(platform, WechatNotifyConstant.WECHAT_CHANGE_NOTICE_TEMPLATE, oldProperties, differences);
            if (StrUtil.isBlank(content)) {
                return ;
            }
            doSend(platform, content);
        });
    }

    @Override
    public void sendAlarmMsg(NotifyTypeEnum typeEnum) {
        BreatheDtpContext breatheDtpContext = BreatheDtpContextHolder.get();
        breatheDtpContext.getPlatform(NotifyPlatformEnum.WECHAT.name()).forEach(platform -> {
            String content = buildAlarmContent(platform, typeEnum, WechatNotifyConstant.WECHAT_ALARM_TEMPLATE);
            if (StrUtil.isBlank(content)) {
                return ;
            }
            doSend(platform, content);
            BreatheDtpContext contextWrapper = BreatheDtpContextHolder.get();
            String dtpName = contextWrapper.getBreatheDtpExecutor().getThreadPoolName();
            BreatheDtpExecutor executor = ThreadPoolExecutorRegistry.get(dtpName);
            ThreadPoolExecutorProperties executorProperties = breatheDtpProperties.getExecutors().stream()
                    .filter(each -> each.equals(dtpName))
                    .findFirst()
                    .orElse(null);
            //能使用ai的话发布这个事件
            if (breatheDtpProperties.isEnabledAi()) {
                AlarmEvent alarmEvent = new AlarmEvent( this, executor, executorProperties);
                ApplicationContextHolder.getInstance().publishEvent(alarmEvent);
            }
        });
    }
    @Override
    protected Pair<String, String> getColors() {
        return new ImmutablePair<>(WechatNotifyConstant.WARNING_COLOR, WechatNotifyConstant.COMMENT_COLOR);
    }
}